package sicnu.cs.aps.common.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import sicnu.cs.aps.common.util.RequestUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenhetao
 * @date 2022/5/27 15:16
 */
@Slf4j
public enum ActivitySignEnum implements BaseEnum {


    /**
     * 未知
     */
    UNKNOWN(0, "未知状态"),
    /**
     * 报名成功但没有填写策划
     */
    SIGN_PASS_NO_PLAN(100, "待完善信息"),
    /**
     * 完成报名信息提交但未审核
     */
    SIGN_COMPLETE(211, "待学院审核"),
    /**
     * 学院审核未通过,报名失败且不可重写
     */
    COLLEGE_AUDIT_REBUT(212, "学院驳回"),
    /**
     * 学院审核驳回,需要重写提交
     */
    UNIVERSITY_AUDIT_ON(221, "学院审核通过"),
    /**
     * 学校审核驳回，修改后可重新提交
     */
    UNIVERSITY_AUDIT_REBUT(222, "学校驳回"),
    /**
     * 活动报名成功
     */
    APPLY_SUCCESS(300, "审核通过"),
    /**
     * 活动进行中
     */
    ACTIVITY_ON(400, "活动进行中"),
    /**
     * 活动待反馈
     */
    ACTIVITY_WAIT_FOR_FEEDBACK(500, "活动待反馈"),
    /**
     * 活动结束
     */
    ACTIVITY_COMPLETE(600, "活动结束"),
    /**
     * 学校审核不通过，失败
     */
    UNIVERSITY_AUDIT_FAIL(711, "学校终止"),
    /**
     * 学院终止
     */
    COLLEGE_AUDIT_FAIL(721, "学院终止"),
    /**
     * 学生取消报名
     */
    STUDENT_CANCELED(731, "学生终止");

    @JsonValue
    Integer state;
    String name;

    ActivitySignEnum() {
    }

    public static List<Integer> analyzeCode(Integer state) {
        if (state == null) {
            log.warn("state为空无须映射");
            return null;

        }
        if (state <= 4) {
            //管理员部分的字段映射
            switch (state) {
                case 1:
                    if (RequestUtil.isCurrentUserACollege()) {
                        return List.of(211);
                    } else if (RequestUtil.isCurrentUserAUniversity()) {
                        return List.of(221);
                    } else {
                        log.warn("用户身份权限有误");
                        return null;
                    }
                case 2:
                    if (RequestUtil.isCurrentUserACollege()) {
                        return List.of(221, 222, 300, 400, 500, 600, 711);
                    } else if (RequestUtil.isCurrentUserAUniversity()) {
                        return List.of(300, 400, 500, 600);
                    } else {
                        log.warn("用户身份权限有误");
                        return null;
                    }
                case 3:
                    if (RequestUtil.isCurrentUserACollege()) {
                        return List.of(212);
                    } else if (RequestUtil.isCurrentUserAUniversity()) {
                        return List.of(222);
                    } else {
                        log.warn("用户身份权限有误");
                        return null;
                    }
                case 4:
                    if (RequestUtil.isCurrentUserACollege()) {
                        return List.of(711);
                    } else if (RequestUtil.isCurrentUserAUniversity()) {
                        return List.of(721);
                    } else {
                        log.warn("用户身份权限有误");
                        return null;
                    }
                default:
                    log.error("未能成功映射");
                    return null;
            }
        } else {
            //卡片查询部分的字段映射
            switch (state) {
                case 100:
                    return List.of(100);
                case 200:
                    return List.of(211, 221, 222, 212);
                case 211:
                    return List.of(211);
                case 212:
                    return List.of(212);
                case 221:
                    return List.of(221);
                case 222:
                    return List.of(222);
                case 300:
                    return List.of(300);
                case 400:
                    return List.of(400);
                case 500:
                    return List.of(500);
                case 600:
                    return List.of(600);
                case 700:
                    return List.of(711, 721, 731);
                case 711:
                    return List.of(711);
                case 721:
                    return List.of(721);
                case 731:
                    return List.of(731);
                default:
                    log.error("未能成功映射");
                    return null;
            }
        }
    }

    public String getDesc() {
        return this.name;
    }

    ActivitySignEnum(Integer state, String name) {
        this.state = state;
        this.name = name;
    }

    @Override
    public int getIndex() {
        return this.state;
    }

    private static final Map<Integer, ActivitySignEnum> MAP = new HashMap<>();

    static {
        for (ActivitySignEnum order : values()) {
            MAP.put(order.state, order);
        }
    }

    public static ActivitySignEnum getEnum(Integer state) {
        return MAP.get(state);
    }

    public static Boolean isCollegeEvent(ActivitySignEnum activitySignEnum) {
        int index = activitySignEnum.getIndex();
        return index == 212 || index == 221 || index == 721;
    }

    public static Boolean isUniversityEvent(ActivitySignEnum activitySignEnum) {
        int index = activitySignEnum.getIndex();
        return index == 300 || index == 711 || index == 222;
    }

}